package com.base.data.elasticsearch.util;

import org.elasticsearch.client.HttpAsyncResponseConsumerFactory;


/**
 * @author magina
 */
public class HttpAsyncResponseConsumerFactoryUtil {

    //default buffer limit is 150MB
    static final int DEFAULT_BUFFER_LIMIT = 150 * 1024 * 1024;

    private volatile static HttpAsyncResponseConsumerFactory factory;

    public static HttpAsyncResponseConsumerFactory getFactory(int bufferLimit) {
        if (factory == null) {
            synchronized (HttpAsyncResponseConsumerFactoryUtil.class) {
                if (factory == null) {
                    factory= new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory(DEFAULT_BUFFER_LIMIT);
                    if(bufferLimit > 0) {
                        factory = new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory(bufferLimit * 1024 * 1024);
                    }
                }
            }
        }
        return factory;
    }

}
